/// @tags: BIT
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
using std::min;
using std::swap;

namespace BlueQuantum {

typedef long long ll;

int const N = 2e5 + 5;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

int n, m, a[N], b[N], diff[N];
ll val[N], ans;

inline int lowbit(int x) { return x & (-x); }

inline void modify(int x, ll v) {
  while (x <= n) { val[x] += v, x += lowbit(x); }
}

inline ll query(int x) {
  ll res = 0;
  while (x > 0) { res += val[x], x -= lowbit(x); }
  return res;
}

inline int main() {
  read(n), read(m);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
    b[i] = i - 1 - query(a[i]);
    ans += b[i], ++diff[b[i]];
    modify(a[i], 1);
  }
  memset(val, 0, sizeof(val));
  modify(1, ans);
  for (int i = 1, tmp = 0; i <= n; ++i) {
    tmp += diff[i - 1];
    modify(i + 1, -(n - tmp));
  }
  for (int i = 1, opt, x; i <= m; ++i) {
    read(opt), read(x);
    if (opt == 1) {
      if (a[x] < a[x + 1]) {
        swap(a[x], a[x + 1]);
        swap(b[x], b[x + 1]);
        modify(1, 1);
        modify(++b[x + 1] + 1, -1);
      } else {
        swap(a[x], a[x + 1]);
        swap(b[x], b[x + 1]);
        modify(1, -1);
        modify(b[x]-- + 1, 1);
      }
    } else
      printf("%lld\n", query(min(x, n - 1) + 1));
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P6186 [NOI Online #1 提高组] 冒泡排序.in", "r", stdin);
  freopen("P6186 [NOI Online #1 提高组] 冒泡排序.out", "w", stdout);
#endif
#endif

  return BlueQuantum::main();
}